package test;

import static org.junit.Assert.*;

import java.util.ArrayList;

import movement.Location;

import org.junit.Before;
import org.junit.Test;

import unit.*;

import dispatch.DispatchCenter;
import emergency.*;

public class testEmergencyPolicy {
	EmergencyPolicyASAP empASAP = new EmergencyPolicyASAP();
	EmergencyPolicyDefault empDEF = new EmergencyPolicyDefault();
	ArrayList<Unit> units = new ArrayList<Unit>();
	Garage garage;
	DispatchCenter center;
	Emergency em1, em2, em3, em4;
	Unit u1, u2, u3, u4, u5, u6;

	@Before
	public void setUp() throws Exception {
		center = new DispatchCenter("DC", new Location(30, 40, "Jajaa"),
				new Location(70, 70, "Garage"));
		garage = center.getGarage();

		u1 = new PoliceCar("1", 95, center.getGarage());
		u2 = new PoliceCar("2", 180, center.getGarage());
		u3 = new Ambulance("3", 120, center.getGarage());
		u4 = new Ambulance("4", 111, center.getGarage());
		u5 = new FireTruck("5", 150, center.getGarage(), Fire.Size.HOUSE);
		u6 = new FireTruck("6", 90, center.getGarage(), Fire.Size.FACILITY);

		units.add(u1);
		units.add(u2);
		units.add(u3);
		units.add(u4);
		units.add(u5);
		units.add(u6);

		em1 = new Fire(new Location(10, 10, "em1"), Severity.SERIOUS, center,
				Fire.Size.FACILITY, false, 2, 1);
		em2 = new PublicDisturbance(new Location(100, 100, "em2"),
				Severity.BENIGN, center, 3);
		em3 = new TrafficAccident(new Location(200, 200, "em3"),
				Severity.NORMAL, center, 3, 1);
		em4 = new Robbery(new Location(300, 300, "em4"), Severity.URGENT,
				center, false, true);
	}

	@Test
	public void testFilterUnits() {
		ArrayList<Unit> fastest = empASAP.sortUnits(center.getGarage()
				.getAvailableUnits(), em2);
		assertEquals(fastest.get(0).toString(), u2.toString());
		assertEquals(fastest.get(1).toString(), u5.toString());
		assertEquals(fastest.get(2).toString(), u3.toString());
		assertEquals(fastest.get(3).toString(), u4.toString());
		assertEquals(fastest.get(4).toString(), u1.toString());
		assertEquals(fastest.get(5).toString(), u6.toString());

		u1.setCurrentLocation(new Location(200, 400, "Loc"));
		u2.setCurrentLocation(new Location(180, 320, "Loc"));
		u3.setCurrentLocation(new Location(500, 500, "Loc"));
		u4.setCurrentLocation(new Location(305, 300, "Loc"));
		u5.setCurrentLocation(new Location(-200, -200, "Loc"));
		u6.setCurrentLocation(new Location(1000, 1000, "Loc"));
		
		ArrayList<Unit> closest = empDEF.sortUnits(center.getGarage()
				.getAvailableUnits(), em4);

		assertEquals(closest.get(0).toString(), u4.toString());
		assertEquals(closest.get(1).toString(), u2.toString());
		assertEquals(closest.get(2).toString(), u1.toString());
		assertEquals(closest.get(3).toString(), u3.toString());
		assertEquals(closest.get(4).toString(), u5.toString());
		assertEquals(closest.get(5).toString(), u6.toString());
	}

	@Test
	public void testTypeEmergencyPolicy() {
		assertEquals(em1.getEmergencyPolicyString(), "ASAP Emergencypolicy");
		assertEquals(em2.getEmergencyPolicyString(), "Default Emergencypolicy");
		assertEquals(em3.getEmergencyPolicyString(), "Default Emergencypolicy");
		assertEquals(em4.getEmergencyPolicyString(), "ASAP Emergencypolicy");
	}
}
